package com.bff.gaia.streaming.api.mo.gaia.api;

import com.bff.gaia.streaming.api.mo.gaia.io.basiccoder.StringDoubleArrayCoder;
import com.bff.gaia.streaming.api.mo.gaia.io.basiccoder.StringDoubleCoder;
import com.bff.gaia.streaming.api.mo.gaia.io.compondedcoder.MaxMinArrayCoder;
import com.bff.gaia.streaming.api.mo.gaia.io.compondedcoder.MaxMinCoder;
import com.bff.gaia.streaming.api.mo.gaia.network.client.AClient;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;

public class Client extends AClient {

	private Double valueToSend;
	private Double[] valueToSendArray;
	private Double max;
	private Double[] maxArray;
	private Double min;
	private Double[] minArray;

	public Client(String host, int port, String key, String cmd) {
		super(host, port, key, cmd);
	}

	public Client(String host, int port, String key, Double valueToSend) {
		super(host, port, key);
		this.valueToSend = valueToSend;
	}

	public Client(String host, int port, String key, Double[] valueToSendArray) {
		super(host, port, key);
		this.valueToSendArray = valueToSendArray;
	}

	@Override
	protected void dojob(BufferedInputStream in, BufferedOutputStream out) throws IOException {
		StringDoubleCoder keyValueCoder = new StringDoubleCoder(super.getKey(), this.valueToSend, super.getCmd());
		out.write(keyValueCoder.getBytes());
		System.out.printf("[%s]: client send: %s\n", Thread.currentThread().getName(), keyValueCoder.toString());
		out.flush();
		byte[] data = new byte[4096];
		byte[] newData;
		do{
			int len = in.read(data);
			newData = new byte[len];
			System.arraycopy(data, 0, newData, 0, len);
		}while(in.available() != 0);
//		int len = in.read(data);
//		byte[] newData = new byte[len];
//		System.arraycopy(data, 0, newData, 0, len);
		MaxMinCoder maxmincoder = new MaxMinCoder(newData);
		this.max = maxmincoder.getMax();
		this.min = maxmincoder.getMin();
		System.out.printf("[%s]: client receive: %s\n", Thread.currentThread().getName(), maxmincoder.toString());
	}
//	public static void main(String[] args) {
//		System.setProperty("java.net.preferIPv4Stack","true");
//		System.setProperty("java.net.preferIPv6Addresses","false");
//		Client c = new Client("127.0.0.1", 8889, "gaia", "reset");
//		c.connect();
//	}
	@Override
	protected void dojobArray(BufferedInputStream in, BufferedOutputStream out) throws IOException {
		StringDoubleArrayCoder keyValueCoder = new StringDoubleArrayCoder(super.getKey(),this.valueToSendArray, super.getCmd());
		out.write(keyValueCoder.getBytes());
		out.flush();
		byte[] data = new byte[4096];
		int len = in.read(data);
		byte[] newData = new byte[len];
		System.arraycopy(data, 0, newData, 0, len);
		MaxMinArrayCoder maxMinCoder = new MaxMinArrayCoder(newData);
		this.maxArray = maxMinCoder.getMax();
		this.minArray = maxMinCoder.getMin();
	}

	public Double getValueToSend() {
		return valueToSend;
	}

	public void setValueToSend(Double valueToSend) {
		this.valueToSend = valueToSend;
	}

	public Double[] getValueToSendArray() {
		return valueToSendArray;
	}

	public void setValueToSendArray(Double[] valueToSendArray) {
		this.valueToSendArray = valueToSendArray;
	}

	public Double getMax() {
		return max;
	}

	public void setMax(Double max) {
		this.max = max;
	}

	public Double[] getMaxArray() {
		return maxArray;
	}

	public void setMaxArray(Double[] maxArray) {
		this.maxArray = maxArray;
	}

	public Double getMin() {
		return min;
	}

	public void setMin(Double min) {
		this.min = min;
	}

	public Double[] getMinArray() {
		return minArray;
	}

	public void setMinArray(Double[] minArray) {
		this.minArray = minArray;
	}
}